<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use app\admin\validate\MarketSocialMediaAccount as MarketSocialMediaAccountValidate;
use app\admin\service\MarketSocialMediaAccount as MarketSocialMediaAccountService;

class MarketSocialMediaAccount
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['account'] =  $request->param('account', null);
        $param['status'] =  $request->param('status', null);
        $param['channel_id'] =  $request->param('channel_id', null);
        $param['language'] =  $request->param('language', null);
        $param['country'] =  $request->param('country', null);
        $param['account_group_id'] =  $request->param('account_group_id', null);
        $param['windows_id'] =  $request->param('windows_id', null);
        $param['start_time'] =  $request->param('start_time', null);
        $param['end_time'] =  $request->param('end_time', null);

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $service->index($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('save')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['channel_id'] =  $request->param('channel_id', '');
        $param['language'] = $request->param('language', '');
        $param['country'] = $request->param('country', '');
        $param['account'] = $request->param('account', '');
        $param['pass'] =  $request->param('pass', '');
        $param['appkey'] = $request->param('appkey', '');
        $param['secret'] = $request->param('secret', '');
        $param['status'] = $request->param('status', '');
        $param['prompt'] = $request->param('prompt', '');
        $param['account_group_id'] = $request->param('account_group_id', '');
        $param['windows_id'] = $request->param('windows_id', null);

        $arrData = $service->save($param);
        return json($arrData);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request,MarketSocialMediaAccountService $service,MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $arrData = $service->read($param);
        return json($arrData);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('update')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] =  $request->param('id', null);
        $param['channel_id'] = $request->param('channel_id', '');
        $param['language'] = $request->param('language', '');
        $param['country'] = $request->param('country', '');
        $param['account'] = $request->param('account', '');
        $param['pass'] = $request->param('pass', '');
        $param['appkey'] = $request->param('appkey', '');
        $param['secret'] = $request->param('secret', '');
        $param['status'] = $request->param('status', '');
        $param['prompt'] = $request->param('prompt', '');
        $param['account_group_id'] = $request->param('account_group_id', '');
        $param['windows_id'] = $request->param('windows_id', null);

        $arrData = $service->update($param);
        return json($arrData);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['ids'] =  $request->param('ids', null);

        $arrData = $service->delete($param);
        return json($arrData);
    }

    /**
     * 获取资源
     * @param Request $request
     * @param MarketSocialMediaAccountService $service
     * @param MarketSocialMediaAccountValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 16:46:18
     * author: jiang
     */
    public function resource(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate){
        $param['field'] =  $request->param('field', 'ALL');
        $arrData = $service->resource($param);
        return json($arrData);
    }

    public function status(Request $request, MarketSocialMediaAccountService $service, MarketSocialMediaAccountValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('status')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['ids'] = $request->param('ids', []);
        $param['status'] = $request->param('status', null);

        $arrData = $service->status($param);
        return json($arrData);
    }
}
